package cn.mengfly.testnrz.netty.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class NettyOpenBoxDecoder {

    static String spliter = "\r\n";
    static String decoderSpliter = "---";

    static String content = "测试的数据";

    public static void testLineBasedFrameDecoder() {
        try {
            ChannelInitializer<EmbeddedChannel> ri = new ChannelInitializer<EmbeddedChannel>() {
                @Override
                protected void initChannel(EmbeddedChannel ch) {
                    ch.pipeline().addLast(new LineBasedFrameDecoder(1024));
                    ch.pipeline().addLast(new StringDecoder());
                    ch.pipeline().addLast(new StringProcessHandler());
                }
            };
            EmbeddedChannel channel = new EmbeddedChannel(ri);
            for (int j = 0; j < 100; j ++) {
                int random = new Random().nextInt(3) + 1;
                ByteBuf buf = Unpooled.buffer();
                for (int i = 0; i < random; i++) {
                    buf.writeBytes(content.getBytes(StandardCharsets.UTF_8));
                }
                buf.writeBytes(spliter.getBytes(StandardCharsets.UTF_8));
                channel.writeInbound(buf);
            }
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException | UnsupportedClassVersionError e) {
            e.printStackTrace();
        }
    }


    public static void testDelimiterBasedFrameDecoder() {
        try {
            ChannelInitializer<EmbeddedChannel> ri = new ChannelInitializer<EmbeddedChannel>() {
                @Override
                protected void initChannel(EmbeddedChannel ch) {
                    ByteBuf delimiter = Unpooled.copiedBuffer(decoderSpliter.getBytes(StandardCharsets.UTF_8));
                    ch.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
                    ch.pipeline().addLast(new StringDecoder());
                    ch.pipeline().addLast(new StringProcessHandler());
                }
            };
            EmbeddedChannel channel = new EmbeddedChannel(ri);
            for (int j = 0; j < 100; j ++) {
                int random = new Random().nextInt(3) + 1;
                ByteBuf buf = Unpooled.buffer();
                for (int i = 0; i < random; i++) {
                    buf.writeBytes(content.getBytes(StandardCharsets.UTF_8));
                }
                buf.writeBytes(decoderSpliter.getBytes(StandardCharsets.UTF_8));
                channel.writeInbound(buf);
            }
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException | UnsupportedClassVersionError e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        testLineBasedFrameDecoder();
        testDelimiterBasedFrameDecoder();
    }
}
